package org.apache.ibatis.ztest.wrapper;

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;

import java.util.Map;

/**
 * @author wangxinyuchuan
 * @version MyMapWrapper.java v1.0 2023/12/22 16:19 wangxinyuchuan Exp $
 */
public class MyMapWrapper extends MapWrapper {

  public MyMapWrapper(MetaObject metaObject, Map<String, Object> map) {
    super(metaObject, map);
  }

  @Override
  public String findProperty(String name, boolean useCamelCaseMapping) {
    if (useCamelCaseMapping && ((name.charAt(0) >= 'A' && name.charAt(0) <= 'Z') || name.contains("_"))) {
      return underlineToCamelCase(name);
    }
    return name;
  }

  //user_name_log_abc_dd
  private static String underlineToCamelCase(String name) {
    StringBuilder sb = new StringBuilder();
    boolean flag = false;
    for (int i = 0; i < name.length(); i++) {
      if (name.charAt(i) == '_') {
        flag = true;
      }else {
        if (flag) {
          sb.append(Character.toUpperCase(name.charAt(i)));
          flag = false;
        }else {
          sb.append(name.charAt(i));
        }
      }
    }
    return sb.toString();
  }
}
